package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ReggieApplication;
import com.itheima.common.MyThreadLocal;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 购物车下单
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) {
        //向订单表中存储订单数据
        //1.1先获取当前登录用户的id
        Long userId = MyThreadLocal.getCurrentId();
        orders.setUserId(userId);
        //1.2设置订单编号--用工具类
        String uuidNumber = IdWorker.get32UUID();
        orders.setNumber(uuidNumber);
        //1.3设置下单时间
        orders.setOrderTime(LocalDateTime.now());
        //1.4设置amount总金额
        BigDecimal bigDecimal = new BigDecimal(0.0);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            Integer number = shoppingCart.getNumber();
            BigDecimal amount = shoppingCart.getAmount();
            //数量和单价相乘
            BigDecimal multiply = amount.multiply(new BigDecimal(number));
            bigDecimal = bigDecimal.add(multiply);
        }
        orders.setAmount(bigDecimal);
        //1.5设置电话号  --根据地址表获得
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(addressBook.getPhone());
        //1.6设置地址
        orders.setAddress(addressBook.getDetail());
        //1.7设置收件人
        orders.setConsignee(addressBook.getConsignee());
        //1.8向订单表中存储订单数据
        save(orders);
        //向订单明细表中存储订单明细--购物车的购物项
        OrderDetail orderDetail = new OrderDetail();
        for (ShoppingCart shoppingCart : shoppingCarts) {
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetailService.save(orderDetail);
        }
        //存储成功后删除购物车的数据
        shoppingCartService.remove(queryWrapper);
    }

}
